'use strict';

const BaseController = require('./base');
const serviceName = 'member';

class MemberController extends BaseController {
  constructor(ctx) {
    super(ctx, serviceName);
  }

  // 注册
  async register() {
    const { ctx } = this;
    const data = ctx.request.body;
    
    // 参数验证
    ctx.validate({
      phone: { type: 'string', format: /^1[3-9]\d{9}$/, required: true },
      password: { type: 'string', min: 6, max: 20, required: true },
      nickname: { type: 'string', required: false },
    });
    
    const result = await ctx.service.member.register(data);
    ctx.success(result);
  }

  // 登录
  async login() {
    const { ctx } = this;
    const data = ctx.request.body;
    
    // 参数验证
    ctx.validate({
      phone: { type: 'string', format: /^1[3-9]\d{9}$/, required: true },
      password: { type: 'string', required: true },
    });
    
    const result = await ctx.service.member.login(data);
    ctx.success(result);
  }

  // 修改密码
  async changePassword() {
    const { ctx } = this;
    const data = ctx.request.body;
    const memberId = ctx.state.member.id;
    
    // 参数验证
    ctx.validate({
      oldPassword: { type: 'string', required: true },
      newPassword: { type: 'string', min: 6, max: 20, required: true },
    });
    
    await ctx.service.member.changePassword(memberId, data);
    ctx.success();
  }

  // 重置密码
  async resetPassword() {
    const { ctx } = this;
    const data = ctx.request.body;
    
    // 参数验证
    ctx.validate({
      phone: { type: 'string', format: /^1[3-9]\d{9}$/, required: true },
      code: { type: 'string', required: true },
      newPassword: { type: 'string', min: 6, max: 20, required: true },
    });
    
    await ctx.service.member.resetPassword(data);
    ctx.success();
  }

  // 获取当前登录会员信息
  async getCurrentMember() {
    const { ctx } = this;
    const memberId = ctx.state.member.id;
    
    const member = await ctx.service.member.findOne(memberId);
    ctx.success(member);
  }

  // 小程序登录
  async wechatLogin() {
    const { ctx } = this;
    const { code, phone, nickname, avatar } = ctx.request.body;
    
    // 调用微信登录接口获取openid
    // TODO: 实现微信登录逻辑
    const openid = 'test_openid'; // 这里需要替换为实际的微信登录逻辑
    
    // 查找或创建会员
    let member = await ctx.service.member.findByOpenid(openid);
    if (!member) {
      member = await ctx.service.member.create({
        openid,
        phone,
        nickname,
        avatar,
        status: 1
      });
    }
    
    // 生成token
    const token = await ctx.service.auth.createToken(member.id);
    ctx.success({ token, member });
  }

  // 重写getPageQuery方法，支持按手机号和昵称搜索
  getPageQuery(query) {
    const where = {};
    if (query.phone) {
      where.phone = {
        $like: `%${query.phone}%`
      };
    }
    if (query.nickname) {
      where.nickname = {
        $like: `%${query.nickname}%`
      };
    }
    if (query.status !== undefined) {
      where.status = query.status;
    }
    return where;
  }
}

module.exports = MemberController;
